<?php

namespace app\admin\model;

use think\Model;

class PromGoods extends Model
{
    // 表名
    protected $name = 'prom_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'starttime_text',
        'endtime_text',
        'status_text',
        'is_end_text',
        'recommend_text',
        'type_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getTypeList()
    {
        return ['0' => __('Type 0'),'1' => __('Type 1'),'2' => __('Type 2')];
    }  
    public function getTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['type'];
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'),'1' => __('Status 1')];
    }     

    public function getIsEndList()
    {
        return ['0' => __('Is_end 0'),'1' => __('Is_end 1')];
    }     

    public function getRecommendList()
    {
        return ['0' => __('Recommend 0'),'1' => __('Recommend 1')];
    }     


    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['starttime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['endtime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsEndTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['is_end'];
        $list = $this->getIsEndList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRecommendTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['recommend'];
        $list = $this->getRecommendList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setStarttimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setEndtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function getGoodsList(){
        $model = model('goods');
        $data = $model->getNoPromGoodsList();
        return $data;
    }

    //促销商品列表
    public function getPromGoodsList($ids){
        $model = model('goods');
        $data = $model->where(['prom_type' => '4','prom_id' => $ids])->order('id desc')->paginate(6);
        return $data;
    }

    


}
